<?php

// 上传游戏结果
require_once 'common/common.php';
require_once 'common/conn.php';

requestURL($_SERVER['REQUEST_URI']);

if (isset($_GET['usercode'])) {
    $usercode = $_GET['usercode'];
}
if (isset($_GET['opponent'])) {
    $opponent = $_GET['opponent'];
}
if (isset($_GET['gametime'])) {
    $gametime = date("Y-m-d H:i:s", trim($_GET['gametime']));
}
// 1 胜利 -1 失败 0 平局
if (isset($_GET['result'])) {
    $result = $_GET['result'];
}
if (isset($_GET['gold'])) {
    $gold = $_GET['gold'];
}
$result_array = array();
if (isset($usercode) && isset($opponent) && isset($gametime) && isset($result) && isset($gold)) {
    $db = new DBC();
    $sql = "SELECT deviceid FROM t_device WHERE devicecode = '" . $usercode . "'";
    $data = $db->fetchData($sql);
    if ($data != false && count($data) > 0) {
        $deviceid = $data[0]['deviceid'];
        $table = "t_game_record_" . $deviceid;
        $sql = "SHOW TABLES LIKE '" . $table . "'";
        $data = $db->fetchData($sql);
        $is_table_exist = false;
        if ($data != false && count($data) > 0) {
            $is_table_exist = true;
        }
        if (!$is_table_exist) {
            $sql = "CREATE TABLE `" . $table . "` (
            `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
            `usercode` varchar(50) DEFAULT NULL COMMENT 'uid,自动生成',
            `opponent` varchar(50) DEFAULT NULL COMMENT '对手',
            `gold` int(11) DEFAULT '0' COMMENT '获得金币数量',
            `gametime` datetime DEFAULT NULL COMMENT '游戏时间',
            `duration` int(11) DEFAULT '0' COMMENT '持续时间',
            `result` int(11) DEFAULT NULL COMMENT '1 胜利 -1 失败 0 平局',
            `right` int(11) DEFAULT '0' COMMENT '正确数',
            `wrong` int(11) DEFAULT '0' COMMENT '错误数',
            PRIMARY KEY (`id`)
            ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
            $db->execute($sql);
        }
        $sql = "INSERT INTO " . $table . "(usercode,opponent,gametime,result,gold) VALUES('" . $usercode . "','" . $opponent . "','" . $gametime . "'," . $result . "," . $gold . ")";
        if ($db->execute($sql)) {
            $sql = "INSERT INTO t_a_game_record(usercode,opponent,gametime,result,createTime) VALUES('" . $usercode . "','" . $opponent . "','" . $gametime . "'," . $result . ",now())";
            $db->execute($sql);
            $result_array["errorNo"] = "0";
            $result_array["errorMsg"] = "";
            $gold_sql = ",gold = gold + " . $gold;

            /* 记录成就开始 */
            if ($result == 1) {
                $sql = "UPDATE t_device SET keepwin = keepwin+1,keeplost = 0" . $gold_sql . " WHERE devicecode = '" . $usercode . "'";
            } else if ($result == -1) {
                $sql = "UPDATE t_device SET keepwin = 0,keeplost = keeplost+1" . $gold_sql . " WHERE devicecode = '" . $usercode . "'";
            } else if ($result == 0) {
                $sql = "UPDATE t_device SET keepwin = 0,keeplost = 0" . $gold_sql . " WHERE devicecode = '" . $usercode . "'";
            }
            if ($db->execute($sql)) {
                $sql = "SELECT keepwin,keeplost FROM t_device WHERE usercode = '" . $usercode . "'";
                $data = $db->fetchData($sql);
                if (checkArrayNotEmpty($data)) {
                    
                }
            }
            /* 更新成就开始 */
            finishGame($table, $usercode);
            winGame($table, $usercode);
            finishInOneDay($table, $usercode, $result);

            /* 更新成就结束 */
            /* 记录成就结束 */
        } else {
            $result_array["errorNo"] = "1";
            $result_array["errorMsg"] = "添加数据错误";
        }
    } else {
        $result_array["errorNo"] = "1";
        $result_array["errorMsg"] = "UID:" . $usercode . " 不存在.";
    }
}
echo json_encode($result_array);

function finishGame($table, $usercode) {
    $db = new DBC();
    $sql = "SELECT COUNT(id) AS total FROM " . $table;
    $data = $db->fetchData($sql);
    if (checkArrayNotEmpty($data)) {
        $num = $data[0]['total'];
        if ($num == 1) {
            updateAchieve($usercode, "achieve10");
        } else if ($num == 50) {
            updateAchieve($usercode, "achieve20");
        } else if ($num == 200) {
            updateAchieve($usercode, "achieve30");
        } else if ($num == 1000) {
            updateAchieve($usercode, "achieve40");
        }
    }
}

function winGame($table, $usercode) {
    $db = new DBC();
    $sql = "SELECT COUNT(id) AS total FROM " . $table . " WHERE result = 1";
    $data = $db->fetchData($sql);
    if (checkArrayNotEmpty($data)) {
        $num = $data[0]['total'];
        if ($num == 1) {
            updateAchieve($usercode, "achieve50");
        } else if ($num == 50) {
            updateAchieve($usercode, "achieve60");
        } else if ($num == 200) {
            updateAchieve($usercode, "achieve70");
        } else if ($num == 1000) {
            updateAchieve($usercode, "achieve80");
        }
    }
}

function finishInOneDay($table, $usercode, $result) {
    $db = new DBC();
    $sql = "SELECT COUNT(id) AS total FROM " . $table . " WHERE result = " . $result . " AND DATE(now()) = DATE(gametime)";
    $data = $db->fetchData($sql);
    if (checkArrayNotEmpty($data)) {
        $num = $data[0]['total'];
        if ($num == 5) {
            if ($result == 1) {
                updateAchieve($usercode, "achieve190");
            } else if ($result == -1) {
                updateAchieve($usercode, "achieve200");
            } else if ($result == 0) {
                updateAchieve($usercode, "achieve210");
            }
        }
    }
}

/*
 * achieve10	完成1局对战
  achieve20	完成50局对战
  achieve30	完成200局对战
  achieve40	完成1000局对战

  胜局
  achieve50	获得1次对战胜利
  achieve60	获得50次对战胜利
  achieve70	获得200次对战胜利
  achieve80	获得1000次对战胜利

  最大连续正确题数
  achieve120	连续正确30道题

  单局成就
  achieve130	在10回合时满血获得胜利
  achieve140	在10回合以内击倒对手
  achieve150	对战双方均无错题直到终局

  achieve160	只用2回合就获得对战胜利
  achieve170	在第10回合赢得胜利
  achieve180	在第10回合输掉游戏


  achieve190	一天内获得5次以上的对战胜利
  achieve200	一天内失败5局以上
  achieve210	一天内获得5次以上的平局

  按类型给大师：
  achieve220	正确回答所有普通包的题目
  achieve230	正确回答所有动物包的题目
  achieve240	正确回答所有音乐包的题目
  achieve250	正确回答所有行为习惯包的题目
  achieve260	正确回答所有故事包的题目

  暂不给编号
  暂不给编号
  achieve270	正确500道不同的题目
  achieve280	正确1000道不同的题目
  achieve290	错误500道不同的题目
  achieve300	错误1000道不同的题目

  achieve310	使用50次嘲讽道具
  achieve320	使用50次赞美道具
  achieve330	被别人赞美超过100次

  achieve340	一局游戏中购买道具花费超过70乐币

  achieve350	第一次使用专题题包对战

  achieve360	购买道具花费超过500乐币
  achieve370	购买道具花费超过1000乐币
  achieve380	购买道具花费超过5000乐币

  achieve390	使用10次以上增加时间道具
  achieve400	使用50次以上增加时间道具
  achieve410	使用10次以上增加生命道具
  achieve420	使用50次以上增加生命道具
  achieve430	使用10次以上炸弹道具
  achieve440	使用50次以上炸弹道具
  achieve450	使用10次以上橡皮擦道具
  achieve460	使用50次以上橡皮擦道具

  连败
  achieve470	连续失败5局

 */
?>
